Public Class cDiversityValueArray

    Dim pDiversitiesBaseOne() As Double
    Dim pCount As Integer
    Dim pTotal As Double

    Public Sub AppendResult(ByVal Diversity As Double)

        pCount = pCount + 1
        ReDim Preserve pDiversitiesBaseOne(pCount)
        pDiversitiesBaseOne(pCount) = Diversity
        pTotal = pTotal + Diversity

    End Sub

    Friend ReadOnly Property Count() As Integer
        Get
            Return pCount
        End Get
    End Property

    Friend Property ResultByIndexBaseOne(ByVal Index As Integer) As Double
        Get
            Return pDiversitiesBaseOne(Index)
        End Get
        Set(ByVal value As Double)
            pDiversitiesBaseOne(Index) = value
        End Set
    End Property

    Friend Function CalculateMean() As Double

        If pCount > 0 Then
            Return (pTotal / pCount)
        End If

    End Function

    Friend Function CalculateStandardDeviation() As Double

        Dim Index As Integer
        Dim LocalMean As Double
        Dim ThisOne As Double
        Dim Accumulator As Double
        Dim SumOfSquares As Double

        LocalMean = CalculateMean()
        For Index = 1 To pCount
            ThisOne = pDiversitiesBaseOne(Index)
            ThisOne = ThisOne - LocalMean
            ThisOne = ThisOne ^ 2
            Accumulator += ThisOne
        Next

        SumOfSquares = Accumulator / (pCount - 1)

        Return (SumOfSquares ^ 0.5)

    End Function

End Class
